package com.northpool.operator.dataset;

import com.alibaba.fastjson.JSON;
import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.annotation.ParamEnum;
import com.northpool.operator.annotation.ParamEnums;
import com.northpool.operator.common.algorithm.OperatorTestBuild;
import com.northpool.operator.common.algorithm.TestParamBuilder;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.param.Parameter;
import com.northpool.operator.param.custom.DatasetParam;
import com.northpool.operator.type.TypeEnum;
import com.northpool.operator.utils.StpOperatorHelper;

import java.util.Map;

@OperatorInfo(
        ref = "DatasetFieldTest",
        version = "0.0.1",
        name = "数据源参数测试",
        description = "数据源连接参数测试",
        enums = {
                @ParamEnums(ref = "dataSourceType", value = {
                        @ParamEnum(ref = "postgresql", name = "PostgreSQL"),
                        @ParamEnum(ref = "kingbase", name = "金仓数据库"),
                        @ParamEnum(ref = "highgo", name = "瀚高数据库"),
                })
        }
)
public class DatasetFieldTest extends StpOperatorHelper {

    @InputParams({
            @InputParam(ref = "type", name = "数据源类型", desc = "数据源类型", type = TypeEnum.ENUM, enumRef = "dataSourceType"),
            @InputParam(ref = "datasource", name = "数据源", desc = "数据源", type = TypeEnum.DATASOURCE),
            @InputParam(ref = "dataset", name = "数据集", desc = "数据集包含数据源ID和数据表名称的JSON描述，如: {\"dataSourceId\":\"xxx-xxx\",\"tableName\":\"table\"}", type = TypeEnum.DATASET, required = true),
            @InputParam(ref = "fields", name = "字段", desc = "数据字段列表，多个逗号隔开，如: f_id,dlmc,dlbm", type = TypeEnum.STRING, required = true),
    })
    public DatasetFieldTest() {
    }

    public static void main(String[] args) throws Exception {
        OperatorTestBuild.execute(DatasetFieldTest.class,
                TestParamBuilder.aBuilder()
                        .param("type", "postgresql")
                        .param("datasource", "kjklz")
                        .param("dataset", "{\"dataSourceId\":\"xxx-xxx\",\"tableName\":\"table\"}")
                        .param("fields", "fid,test")
                        .build());
    }

    @Override
    public void execute(ParamTool paramTool) throws Exception {
        Map<String, Parameter<?>> parameterMap = paramTool.getParameterMap();
        DatasetParam dataset = paramTool.getDataset("dataset");
        String datasource = paramTool.getStr("datasource");
        String jsonString = JSON.toJSONString(parameterMap);
        this.log(jsonString);
    }
}
